package com.panda.cn.utils.result;


import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * @author: Mr.Yan
 * @create: 2020/10/21
 * @description: 返回结果集统一封装
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 3436477890959388499L;

    /**
     * 返回操作码（默认为正常）
     */
    private Integer code = RCodeEnum.SUCCESS.code();

    /**
     * 返回正确消息信息
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String msg = "";

    /**
     * 返回错误的消息信息
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String error = "";

    /**
     * 返回数据信息
     */
    private T data;

    public static R success(){
        return new R().msg("success");
    }
    public static R fail(){
       return new R().code(RCodeEnum.FAIL).error("fail");
    }

    // 构造方法
    public static R build() {
        return new R();
    }

    public static <T> R<T> build(T data) {
        return new R().data(data);
    }

    // 赋值方法
    public R data(T data) {
        this.data = data;
        return this;
    }

    public R code(RCodeEnum enmu) {
        this.code = enmu.code();
        return this;
    }

    public R msg(String msg) {
        this.msg = msg;
        return this;
    }

    public R error(String error) {
        this.error = error;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public String getError() {
        return error;
    }

    public static void main(String[] args) {
        R.success().data("123");


        R.success().data("abc");


        R.success().data("0000");
    }

}

